Page({
  data: {
    vaccine:[],
    statusOptions: ['在读', '请假', '休学', '毕业', '汪星'],
    adoptionOptions: [ '已领养','待领养','不宜领养'],
    sterilizationOptions: ['未绝育', '已绝育'],
    selectedStatus: '请选择状态',
    status:0,
    selectedAdoption: '请选择领养情况',
    adoptable:0,
    selectedSterilization: '请选择绝育情况',
    neutered:0,
  },

  onStatusChange(e) {
    this.setData({
      selectedStatus: this.data.statusOptions[e.detail.value],
      status:e.detail.value
    });
  },
delIt(e){
const index = e.currentTarget.dataset.index
this.data.vaccine.splice(index,1)
const vaccine = this.data.vaccine
this.setData({
  vaccine
})
},
  addVaccine() {
   wx.showModal({
     title:'请输入疫苗信息',
     content:'',
     placeholderText:'请输入日期、种类、次数，用|分割',
     editable:true
   }).then(r=>{
if(r.confirm && r.content){
 const arr =  r.content.split('|')
  const i = {date:arr[0],species:arr[1],frequency:arr[2]}
  console.log(i)
 this.data.vaccine.push(i)
 let vaccine = this.data.vaccine
  this.setData({
    vaccine
  })
}
   })
  },

  // 删除疫苗条目
  removeVaccine(e) {
    const index = e.currentTarget.dataset.index;
    const updatedVaccines = this.data.vaccines.filter((_, i) => i !== index);
    this.setData({
      vaccines: updatedVaccines
    });
  },

  // 更新疫苗类型
  updateVaccineType(e) {
    const index = e.currentTarget.dataset.index;
    const updatedVaccines = [...this.data.vaccines];
    updatedVaccines[index].type = e.detail.value;
    this.setData({ vaccines: updatedVaccines });
  },

  // 更新打针时间
  updateVaccineDate(e) {
    const index = e.currentTarget.dataset.index;
    const updatedVaccines = [...this.data.vaccines];
    updatedVaccines[index].date = e.detail.value;
    this.setData({ vaccines: updatedVaccines });
  },

  // 更新第几针
  updateVaccineDose(e) {
    const index = e.currentTarget.dataset.index;
    const updatedVaccines = [...this.data.vaccines];
    updatedVaccines[index].dose = e.detail.value;
    this.setData({ vaccines: updatedVaccines });
  },
async addPic(){
const res = await getApp().$upload('dog')
this.setData({
  picture_url:res.data
})
},
delPic(){
  wx.showModal({
    title: '提示',
    content: '是否删除当前图片',
    
  }).then(r=>{
    if(r.confirm){
      this.setData({
        picture_url:''
      })
    }
  })
 
},
  onAdoptionChange(e) {
    this.setData({
      selectedAdoption: this.data.adoptionOptions[e.detail.value],adoptable:e.detail.value
    });
  },
onLoad(e){
if(e.dogId){
  this.data.dogId = e.dogId
  this.getInfo()
}

},
changeGender(e){
console.log(e)
this.data.gender = e.detail.value
},
async saveIt(){
const data = {
  picture_url:this.data.picture_url ||'',
  adoptionDate:this.data.adoptionDate,
  neuteredDate:this.data.neuteredDate,
  vaccineStatus:this.data.vaccine.length>0?"已接种":'未接种',
  description:this.data.description,
  gender:this.data.gender,
 name:this.data.name,
 status:this.data.status,
 neutered:this.data.neutered,
 adoptable:this.data.adoptable,
}


if(this.data.dogId){
  const res = await getApp().$request('/api/updateDog','POST',{...data,id:this.data.dogId})
 
}else{
  const res = await getApp().$request('/api/addDog','POST',{...data})
  this.data.dogId = res.data
}

if(this.data.vaccine.length>0){
  const res = await getApp().$request('/api/updateVaccineByDogId','POST',{vaccines:this.data.vaccine,dogId:this.data.dogId})
  }
  wx.redirectTo({
    url: '/pages/dogDetail/dogDetail?dogId='+this.data.dogId,
  })
},
async getInfo(){
const res = await getApp().$request('/api/getDogInfo','POST',{dogId:this.data.dogId,userId:wx.getStorageSync('user').id})
this.setData({
     vaccine:res.data.vaccines,
     gender:res.data.gender,
     adoptionDate:res.data.adoptionDate,
     description:res.data.description,
     adoptable:res.data.adoptable,
     status:res.data.status,
     neutered:res.data.neutered,
    neuteredDate:res.data.neuteredDate,
    selectedStatus: ['在读', '请假', '休学', '毕业', '汪星'][res.data.status],
    picture_url:res.data.picture_url,
    selectedAdoption: ['已领养','待领养','不宜领养' ][res.data.adoptable],
    name:res.data.name,
    selectedSterilization: ['未绝育', '已绝育'][res.data.neutered],
})
console.log(res.data.adoptable,this.data.selectedAdoption)
},

inputName(e){
this.data.name= e.detail.value
},
inputNeuteredDate(e){
  this.data.neuteredDate= e.detail.value
},
inputAdoptionDate(e){
this.data.adoptionDate = e.detail.value
},
inputDesc(e){
  this.data.description = e.detail.value
},
  onSterilizationChange(e) {
    this.setData({
      selectedSterilization: this.data.sterilizationOptions[e.detail.value],neutered:e.detail.value
    });
  }
});
